<template>
  <div class="add">
    <el-upload class="upload-demo" action="http://ergou666.shop:8000/img/post_img/" multiple
      :before-upload="beforeUpload" list-type="picture-card" :data="{ 'favorite': favorite }" :on-success="onSuccess"
      :headers="{ 'token': token }">
      <!-- :http-request="submitUpload" -->
      <div>
        <el-icon class="el-icon--upload">
          <upload-filled />
        </el-icon>
        <div class="el-upload__text">
          点击上传图片
        </div>
      </div>
      <template #file="{ file }">
        <div class="photo-list">
          <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
          <span class="el-upload-list__item-actions">
            <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)">
              <el-icon>
                <zoom-in />
              </el-icon>
            </span>
            <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleDownload(file)">
              <el-icon>
                <Download />
              </el-icon>
            </span>
            <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleRemove(file)">
              <el-icon>
                <Delete />
              </el-icon>
            </span>
          </span>
        </div>
      </template>
    </el-upload>
  </div>
  <el-dialog v-model="dialogVisible">
    <img w-full :src="dialogImageUrl" alt="Preview Image" />
  </el-dialog>
</template>
 
<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { UploadFilled } from '@element-plus/icons-vue'
import { myToken } from "@/tools/api/api";
import { Delete, Download, Plus, ZoomIn } from '@element-plus/icons-vue'
import type { UploadFile, UploadProps } from 'element-plus'
import { usePhotoStore } from "@/store/photo";
const photoStore = usePhotoStore()
//组件通信函数
const emit = defineEmits(['reload'])

onMounted(() => {
  getFavoriteId()
})
/**
 * 获取收藏夹id
 */
const favorite = ref<string>('')
const getFavoriteId = () => {
  if (photoStore.chosedSmallFolderId) {
    favorite.value = photoStore.chosedSmallFolderId
  }
}

/**
 * 上传前
 */

const beforeUpload: UploadProps['beforeUpload'] = (file) => {
  let pictureType = file.name.split('.')[1]
  // if (pictureType !== 'bpmn' && pictureType !== 'jpg' ) {
  //   ElMessage.warning("暂不支持" + pictureType + "文件格式！")
  //   return false
  // }
}

const dialogImageUrl = ref('')
const dialogVisible = ref(false)
const disabled = ref(false)

const handleRemove = (file: UploadFile) => {
  
}

const handlePictureCardPreview = (file: UploadFile) => {
  dialogImageUrl.value = file.url!
  dialogVisible.value = true
}

const handleDownload = (file: UploadFile) => {
  
}


/**
 * 获取token
 */
const token = ref(myToken())


/**
 * 文件上传成功时的钩子	
 */
const onSuccess = (res: any) => {
  
  if (res.code === 100) {
    //success
    ElMessage.success("上传成功")
    // 通知父组件重新渲染界面
    emit('reload', '11')
  } else ElMessage.error(res.msg)
}

</script>


<style lang="scss" scoped>
$item-height: 0.36rem;
$item-width: 0.6rem;
$a-font-size: 0.08rem;

span {
  font-size: 0.05rem;
}

.add {
  height: auto;
  width: $item-width * 8;
  break-inside: avoid;
  border-radius: 0.1rem;

  .upload-demo {
    width: 100%;
    height: $item-height*4;

    .el-icon--upload {
      font-size: $a-font-size*4;
    }

    .el-upload__text {
      font-size: $a-font-size;
      font-weight: bolder;
    }

    ::v-deep(ul) {
      width: 100%;
      display: flex;
      flex-wrap: wrap;
    }

  }

  .el-upload-list__item-info {
    width: $item-width*3 !important;
  }

}
</style>
